package com.cens.mall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.cens.mall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2016102500757109";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCtrZ/4iTrWIfYawZLAQpSNhLJDaRmUXSQS08lkpOL7e1LA3ViUvLavOT+skERuAl7y6lfqynHSD9TuH/1Sa7aGXzsW4K27C4EzdihLIDbh523TjxtAggrdsRjOwjXS+YqWuO0YsgXnDV1xpQI0grTjmo+MTXpexpkVM1R04s2XjWeqUUTO/9/AP813FceI69Jya7qsc7CAkJITMYk3Tscjrwudj8IISV7H4yeIm741duYpms27icpWui8rdOceH3iWu76eOeT/nfvZI/cNMrlhBYWJtEzdhqeGTZOzE7CCm0IwSXuHY4Wg1GXAZtKJM+t6D+jOnidSKfVaImi++9O5AgMBAAECggEAC+FblwG0H5+1GQFyy2oChWe1Vf0jfc2dQRhxp0vMJw25jG3XuFzsVjIhU+mOdMP+wLkdCHeOr6LbmupfRrsNgkWTYi4+VBQmY2HKwdDTUEKndZhNce0zVxTdMI+CZQsi2sqNlWngE30LjlGORA9I1mTlOuC15TPKlvOq7E/zZlFt/DjB+R9uyms829wmL7AAWihh5O+XwrClG74iFaIlftIFz/L4KNgj+6XhMAWd1m42aW9KK3DXgYQlliaJGF3VEKdVs+cUw232ZFldDhMBek2sede06m50j1Hhky2WLB+lqyVpjiHGwH9WdhtITZov4GGcSot352ONTK6Ffz21gQKBgQDkcFJgYR7ySJhT3ARMUJo8tsz7857soMxihvHREmyCgEZXcXJChVF/u9ve6MBzMtDFyIxr2KP1wsNe/aYlYdJEqeRy+vwa1B9vPqwUK7CYRdjQDrUR+oQracny38Gh5+ssbXg4g10nTNqQ+615/2UOUtUwphF0Emr4bM0UUAaTaQKBgQDCofDVXERopokUWookB9N+4cf4uNxDX188fQuXuNQn6qVesDXuwxRgWArkAwBcpyoasFWdSl7lLMvAHZU9B68ONVdNvKpOvNdbayR9jWPGdqXIhh847/OGHhaB3xMqM8utq8y14Q7TmvOqivLiR/7cjLvLZM8HtwL11TAznKFD0QKBgQDbQtROnenOM8/B7hRSC6t/SxZFXvjTtyN2DoBcZIqyV90sYT+CieRH7lCEDHNsHwI0VOIzQaVeZqqPqnamutXQAiG8GxuPxLrHVChm0+iUv7XzPaqerw3dfZTRAeQhOzGHWythUvU+m43OS0OfLXwZ23e1UjC+cE8sU4IkVQt6wQKBgQC8WrnV0WAGorvgLaqoKca8cMX0ZvoKXT6uLBlO8DiolIULOJ5I/RM4FZwcgucTw1aU03dXK8TgJlTcDAUENe5sYqSlf2KVpl0HhI2gz3bydpoDkAChOl3CulVvw7EpU0zWOn92uGF/2OHcdnGNGL2FNND8igds4OiFfZYTLI5IMQKBgQDhbfUCC51lhJDzYanwf/3RGog1dOwHh539q1bppPk1Et2NRn6uC0WuTg2STpnX3VdqBqhFZmUubGW+P2SKRIdx0YDEQhgo6FjZBHvEAWuY7oEoZ+YxtK+rgBZp3rqYZrp8TC3YGwyTZOIP/tTqQRL234LOmKJQVCQCbgpHjUlC0Q==";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgs7DMMAibzpH8qVJP+qMUVXLsjCvGjTuBsUp3irjA6sq6mEFAHuAF90N8JcEhW/R6l3byD+Vw1MCT5FPUEf32CseZ+p1eed7nRL4gRyfIR4ufqL5gG+AWrgoBtWe7eC3aIiBl5GbixHSNAgYh+0wdoOmce/7eMdgBwK7qYshFENL4QdKEvSBULexyx9lgE+IRyk0dT4LPyhTMGXqW8f0JJzRNb8d/ysPqN+JvFAoKYqbQ9IS7vRyczn5d7iTR5/LqXPHAUZnTlPwwU/2iS4zzB8XSwiEgDePmewV1QPlLbz5NOMQbpYuKh7M+J32WHPNm1YakaljA/hKS4xHzA1xXwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://xxxxxx/paayed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.censmall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
